package org.gvp.generator.table;

import org.gvp.common.util.StringUtil;
import org.gvp.generator.constant.Constants;
import org.gvp.generator.log.LogFactory;
import org.gvp.generator.log.Logger;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class TableHandler {
    private final Logger log = LogFactory.getLogger(this.getClass());

    private Connection connection;
    private String dbName;
    private String tableName;
    public TableHandler(Connection connection,String dbName, String tableName) {
        this.connection = connection;
        this.dbName = dbName;
        this.tableName = tableName;
    }

    public TableInfo getTableInfo() {
        log.debug("开始解析数据表信息: " + tableName);
        TableInfo tableInfo = new TableInfo();
        try {
            ResultSet tables = connection.getMetaData().getTables(this.dbName, null, this.tableName, null);
            if(tables.next()){
                String dbName = tables.getString(Constants.DATABASE_NAME);
                String tableType = tables.getString(Constants.TABLE_TYPE);
                String tableRemarks = tables.getString(Constants.REMARKS);
                tableInfo.setDatabaseName(dbName);
                tableInfo.setTableName(tableName);
                tableInfo.setLowerName(StringUtil.lowerCameCase(tableName));
                tableInfo.setUpperName(StringUtil.upperCamelCase(tableName));
                tableInfo.setTableType(tableType);
                tableInfo.setRemark(tableRemarks);
//                log.debug("数据表解析完成: " + tableInfo);
                this.resolveColumns(tableInfo);
            }
        } catch (SQLException e) {
            log.error("数据表解析失败");
            throw new RuntimeException(e);
        }
        return tableInfo;
    }

    private void resolveColumns(TableInfo tableInfo) {
        List<ColumnInfo> columnInfoList = tableInfo.getColumns();
        try {
            ResultSet columns = connection.getMetaData().getColumns(this.dbName, null, tableName, null);
            while (columns.next()){
                ColumnInfo columnInfo = new ColumnInfo();
                String columnName = columns.getString(Constants.COLUMN_NAME);
                String columnType = columns.getString(Constants.TYPE_NAME);
                int columnSize = columns.getInt(Constants.COLUMN_SIZE);
                String columnRemarks = columns.getString(Constants.REMARKS);
                boolean autoInc = columns.getBoolean(Constants.IS_AUTOINCREMENT);
                String defaultValue = columns.getString(Constants.COLUMN_DEF);
                int dataType = columns.getInt(Constants.DATA_TYPE);
                columnInfo.setColumnName(columnName);
                columnInfo.setAutoIncrement(autoInc);
                columnInfo.setRemark(columnRemarks);
                columnInfo.setLowerName(StringUtil.lowerCameCase(columnName));
                columnInfo.setUpperName(StringUtil.upperCamelCase(columnName));
                columnInfo.setJavaType(DataTypeConvert.convertToJava(dataType));
                columnInfo.setTypescriptType(DataTypeConvert.convertToTypescript(dataType));
                columnInfoList.add(columnInfo);
//             log.debug("解析字段信息: " + columnInfo);
            }
        } catch (SQLException e) {
            log.debug("字段解析失败");
            throw new RuntimeException(e);
        }
    }


}
